#coding=utf-8
#LightCam SSH抓数据程序测试版
#init by monitor 20150901

import multiprocessing,subprocess,sys
import socket,os,time,re,traceback,json,binascii
import lc_libs,pymongo,threading


def oper(this):
	RemoteServer=this['RemoteServer']
	BrigdeServer=this['BrigdeServer']
	EventDB=this['EventDB']
	TableName=this['TableName']
	SID=this['SID']
	type=this['type']
	isFile=0
	Agent=""
	
	if "db" in RemoteServer:
		localTable="oramon_"+TableName
	else:
		localTable=TableName
		
	events_db=db.useDatabase(EventDB)
	table=db.UseCollection(events_db,TableName)
	try:
		if RemoteServer in "bizdb1|bizdb2|qrydb1|qrydb2":
			filename=RemoteServer+'-'+SID+'-'+type+'.json'
			comd='LightCamPy lc_batch.py %s get /monitor/LightCam/data/%s %s /monitor/LightCam/data/' % (RemoteServer,filename,filename)
			isFile=1
		else:
			if BrigdeServer=='mondb1':
				comd='ssh %s "source ~/.bash_profile;cd /monitor/LightCam/scripts/;LightCamPy lc_query.py %s %s"' % (RemoteServer,EventDB,TableName)
			elif BrigdeServer=='ywn_monitor1' or BrigdeServer=='yww_monitor1':
				comd='ssh %s "ssh %s "source ~/.bash_profile\\\\\\;cd /monitor/LightCam/scripts/\\\\\\;LightCamPy lc_query.py %s %s""' % (BrigdeServer,RemoteServer,EventDB,TableName)
			elif BrigdeServer=='config3':
				comd='ssh %s "ssh %s "ssh %s "source ~/.bash_profile\\\\\\\\\\\\;cd /monitor/LightCam/scripts/\\\\\\\\\\\\;LightCamPy lc_query.py %s %s"""' % ('ywn_monitor1',BrigdeServer,RemoteServer,EventDB,TableName)
			#print comd
	except Exception:
		excinfo=traceback.format_exc()
		print excinfo
		errorValue=1
		logdate=time.strftime('%b %d %H:%M:%S',time.localtime())
		logfile.write(logdate+" RemoteServer: "+RemoteServer+"\nerror info: "+excinfo+"\n")
		logfile.flush()
		exit()
	
	try:
		popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
		#判断子进程是否结束
		timeflag=0
		while True:
			if popen.poll()==None:
				time.sleep(0.1)
				timeflag=timeflag+1
				if timeflag > 100:
					return "TimeOut"
				else:
					continue
			else:
				break
		if isFile==0:
			line=popen.stdout.read()
			res=json.loads(line)
		else:
			print filename
			res=json.load(open('/monitor/LightCam/data/'+filename,'r'))
		#print line

		databaseStatus="正常"
		print this
	

		if type == 'lc_ipsla':
			for items in res['rows']:
				expr={'Source':items['Source'],'Target':items['Target']}
				datas={'Source':items['Source'],'Target':items['Target'],'Rtt':str(items['Rtt']),'Loss':items['Loss'],'Time':items['Time']}
				sortcont=[("Source",pymongo.ASCENDING),("Target",pymongo.ASCENDING)]
				saveData(expr,datas,sortcont,localTable)
			exit()
		if type == 'tablespace':
			result="正常"
			for items in res['rows']:
				Agent=items['Agent']
				expr={'Type':items['Type'],'Agent':items['Agent'],'SID':items['SID'],'Name':items['Name']}
				datas={'Type':items['Type'],'Agent':items['Agent'],'SID':items['SID'],'Name':items['Name'],'Total':items['Total'],'Free':items['Free'],'Time':items['Time']}
				sortcont=[("Agent",pymongo.ASCENDING),("SID",pymongo.ASCENDING),("Name",pymongo.ASCENDING)]
				saveData(expr,datas,sortcont,localTable)
				if float(items['Free'])<float(items['Total'])*0.3:
					result="异常"
					databaseStatus="异常"
			datas={'Agent':Agent,'tablespace':result}
			saveDBData(Agent,datas)
		if type == 'instancestatus':
			result="正常"
			for items in res['rows']:
				Agent=items['Agent']
				expr={'Type':items['Type'],'Agent':items['Agent'],'SID':items['SID']}
				datas={'Type':items['Type'],'Agent':items['Agent'],'SID':items['SID'],'Status':items['Status'],'Starttime':items['Starttime'],'Time':items['Time']}
				sortcont=[("Agent",pymongo.ASCENDING),("SID",pymongo.ASCENDING)]
				saveData(expr,datas,sortcont,localTable)
				if items['Status'] != "OPEN":
					result="异常"
					databaseStatus="异常"
			datas={'Agent':Agent,'instancestatus':result,'SID':SID}
			saveDBData(Agent,datas)
		if type == 'datafiles':
			result="正常"
			for items in res['rows']:
				Agent=items['Agent']
				expr={'Type':items['Type'],'Agent':items['Agent'],'FILENAME':items['FILENAME']}
				datas={'Type':items['Type'],'Agent':items['Agent'],'FILENAME':items['FILENAME'],'Status':items['Status'],'Time':items['Time']}
				sortcont=[("Agent",pymongo.ASCENDING),("FILENAME",pymongo.ASCENDING)]
				saveData(expr,datas,sortcont,localTable)
				if items['Status'] != "ONLINE" and "system" not in items['FILENAME']:
					result="异常"
					databaseStatus="异常"
			datas={'Agent':Agent,'datafiles':result}
			saveDBData(Agent,datas)
		if type == 'controlfile':
			result="正常"
			for items in res['rows']:
				Agent=items['Agent']
				expr={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME']}
				datas={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME'],'Status':items['Status'],'Time':items['Time']}
				sortcont=[("Agent",pymongo.ASCENDING),("NAME",pymongo.ASCENDING)]
				saveData(expr,datas,sortcont,localTable)
				if items['Status'] != "NORMAL":
					result="异常"
					databaseStatus="异常"
			datas={'Agent':Agent,'controlfile':result}
			saveDBData(Agent,datas)
		if type == 'crs_stat':
			result="正常"
			for items in res['rows']:
				Agent=items['Agent']
				expr={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME']}
				datas={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME'],'TYPE':items['TYPE'],'Type':items['Type'],'STATE':items['STATE'],'HOST':items['HOST'],'TARGET':items['TARGET'],'Time':items['Time']}
				sortcont=[("Agent",pymongo.ASCENDING),("NAME",pymongo.ASCENDING)]
				saveData(expr,datas,sortcont,localTable)
				if items['TARGET']!= "ONLINE":
					result="异常"
					databaseStatus="异常"
			datas={'Agent':Agent,'crs_stat':result}
			saveDBData(Agent,datas)
		if type == 'process':
			result="正常"
			for items in res['rows']:
				Agent=items['Agent']
				expr={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME']}
				datas={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME'],'Status':items['Status'],'NUMBER':items['NUMBER'],'Time':items['Time']}
				sortcont=[("Agent",pymongo.ASCENDING),("NAME",pymongo.ASCENDING)]
				saveData(expr,datas,sortcont,localTable)
				if items['Status']!= "UP":
					result="异常"
					databaseStatus="异常"
			datas={'Agent':Agent,'process':result}
			saveDBData(Agent,datas)
		if type == 'disk':
			result="正常"
			for items in res['rows']:
				Agent=items['Agent']
				expr={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME']}
				datas={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME'],'Status':items['Status'],'USED':items['USED'],'Percent':items['Percent'],'Time':items['Time']}
				sortcont=[("Agent",pymongo.ASCENDING),("NAME",pymongo.ASCENDING)]
				saveData(expr,datas,sortcont,localTable)
				if int(items['Percent']) >= 70:
					result="异常"
					databaseStatus="异常"
			datas={'Agent':Agent,'disk':result}
			saveDBData(Agent,datas)
		if type == 'config':
			result="正常"
			for items in res['rows']:
				Agent=items['Agent']
				expr={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME']}
				datas={'Type':items['Type'],'Agent':items['Agent'],'NAME':items['NAME'],'SIZE':items['SIZE'],'Status':items['Status'],'PERMISSION':items['PERMISSION'],'GID':items['GID'],'MTIME':items['MTIME'],'UID':items['UID'],'Time':items['Time']}
				sortcont=[("Agent",pymongo.ASCENDING),("NAME",pymongo.ASCENDING)]
				saveData(expr,datas,sortcont,localTable)
				if int(items['Status']) >= 10000:
					result="异常"
					databaseStatus="异常"
			datas={'Agent':Agent,'config':result}
			saveDBData(Agent,datas)
		datas={'Agent':Agent,'databaseStatus':databaseStatus}
		saveDBData(Agent,datas)

	except Exception:
		excinfo=traceback.format_exc()
		print excinfo
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" :\n"+excinfo+"\n")
		logfile.flush()


def saveData(expr,datas,sortcont,tablename):
	status_db.drop_collection(tablename)
	table=db.UseCollection(status_db,tablename)
	opts=db.queryData(table,expr,sortcont,0)
	res=db.genResult(opts)
	if len(res)==0:db.insertDatas(table,datas)
	else:db.updateData(table,expr,datas)
	
def saveDBData(Agent,datas):
	tablename="database"
	status_db.drop_collection(tablename)
	expr={'Agent':Agent}
	sortcont=[("Agent",pymongo.ASCENDING)]
	table=db.UseCollection(status_db,tablename)
	opts=db.queryData(table,expr,sortcont,0)
	res=db.genResult(opts)
	if len(res)==0:db.insertDatas(table,datas)
	else:db.updateData(table,expr,datas)


if __name__ == "__main__":
	
	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()

	agent_hostname=lc_libs.lc_gethostname()

	try:
		#初始化mongo数据库
		db=lc_libs.lc_mongodb_operater('127.0.0.1',27017)

		db.connect()
		Conn=db.getConn()
		if Conn==None:
			print "Can not Connect to the MondbDB"
			exit(0)
		
		status_dbname='lc_status'
		status_db=db.useDatabase(status_dbname)

	except Exception:
		excinfo=traceback.format_exc()
		logdate=time.strftime('%b %d %H:%M:%S',time.localtime())
		logfile.write(logdate+" 初始化mongo数据库失败:\n"+excinfo+"\n")
		logfile.flush()
		exit(0)

	MonitorList = []
	
	for lines in open(confname):
		if lines[0] == '#' or lines.split("\t")[0] not in (agent_hostname,'default'):
			continue

		line=lines.strip('\r\n').split('\t')
		dconf={}

		for t in line[4].split(","):
			#print t
			#dconf['agent_hostname']=line[0]
			dconf['RemoteServer']=line[1]
			dconf['BrigdeServer']=line[3]
			dconf['SID']=line[2]
			dconf['type']=t
			dconf['EventDB']=line[5]
			dconf['TableName']=t
			dconf['interval']=line[6]
			dconf['schedule']=line[7]
			dconf['holiday']=line[8]
			MonitorList.append(dconf)
			dconf={}
			

	timeflag=int(time.time())


	
	while True:
		time.sleep(1)
		timeflag+=1
		realtime=int(time.time())
		#print realtime

		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())

		threads = []

		for i in MonitorList:
			#holiday_state = lc_libs.lc_isholiday()
			#if int(i['holiday']) == 1 and holiday_state == True:
			#	continue

			time_state = lc_libs.lc_schedule(i['schedule'])
			if time_state == False:
				continue
			
			flag=realtime%int(i['interval'])
			print flag
			if flag<=realtime-timeflag:
				t = threading.Thread(target=oper,args=(i,))
				#print i
				threads.append(t)

		timeflag=int(time.time())
		for t in threads:
			t.start()
			print "aaa"
			while True:
				#print len(threading.enumerate())
				time.sleep(0.1)
				if(len(threading.enumerate()) < 10):
					break 
